package cn.lxb.netstat.collector;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.lxb.netstat.collector.NetstatCollectorProperties.Type;

/**
 * @author TFB
 */
@Configuration
@EnableConfigurationProperties(NetstatCollectorProperties.class)
public class CollectorConfiguration {

	@Autowired
	NetstatCollectorProperties properties;

	@Bean
	AddressRepository addressRepository() throws IOException {
		Type type = properties.persist.type;
		if (type == Type.MEMORY) {
			return new MemoryAddressRepository();
		} else if (type == Type.FILE) {
			return new FileAddressRepository(properties);
		}
		throw new IllegalStateException("Unsupported persist type: " + type);
	}

}
